package com.iweb.controller;

import com.common.context.RequestMapping;
import com.iweb.pojo.Admin;
import com.iweb.pojo.Good;
import com.iweb.service.GoodService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Random;

@RequestMapping("good")
public class GoodController {


    @RequestMapping("selectGoodList")
    public String selectGoodList(Good good, HttpServletRequest request) {
//        System.out.println(good);
        GoodService goodService = new GoodService();
        String title = null;
        if (good != null) {
            title = good.getTitle();
        }
        List<Good> list = goodService.selectGoodList(title);
        int len = list.size();
        for (int i = 0; i < 28 - len; i++) {
            list.add(list.get(new Random().nextInt(list.size())));
        }
        request.setAttribute("list", list);
//        System.out.println(list);
        return "/good/list.jsp";
    }

    @RequestMapping("selectOrderList")
    public String selectOrderList(Admin admin, HttpServletRequest request) {

        GoodService goodService = new GoodService();
        String aid = null;
        if (admin != null) {
            aid = admin.getAid();
        }
        List<Good> list = goodService.selectOrderList(aid);
//        int len = list.size();
//        for (int i = 0; i < 30-len; i++) {
//            list.add(list.get(new Random().nextInt(list.size())));
//        }

        request.setAttribute("list", list);
        System.out.println("！！！！" + list);
        return "/user/shopCar.jsp";
    }

    /*根据gid查询商品详情*/
    @RequestMapping("selectById")
    public String selectById(Good good, HttpServletRequest request) {
        GoodService goodService = new GoodService();
        String gid = null;
        if (good != null) {
            gid = good.getGid();
        }
        good = goodService.selectGoodList2(gid);
        request.setAttribute("good", good);
        return "/good/details.jsp";
    }

}
